%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================
 
%========================================================================== 
% List of the files on which this procedure depends: 
% 
% none
% 
%==========================================================================

%==========================================================================
% STOP. DO NOT EDIT THIS FILE BEFORE READING THE REMARK
%==========================================================================
 
%========================================================================== 
% The function "isRegistered" finds the names of the computer and the user
% running it, and finds out if they both can be found in a fixed,
% hard-coded list of authorized computers and users.
% INPUT: None.
% OUTPUT: "isRegisteredUser" returns a logical specifying whether the
% machine and user running this code are authorized to use it.
% REMARK: If this file is called "isRegistered.m" do not edit it. Instead, 
% edit the companion file called "isRegisteredBlank.txt".
%========================================================================== 
function isRegisteredUser = isRegistered()

registeredCellArr = { ...
    'petrushev'; ... 
    'mirctech'; ... 
    'MyrnaLoy'; ... 
    'wilsbacher'; ... 
    'scott'; ... 
    };


%==========================================================================
% STOP. DO NOT EDIT THIS FILE BEFORE READING THE REMARK
%==========================================================================

% Define the message to be shown in case an unauthorized user attempts to 
% run the code.
unauthorizedUserMessageCellArr = { ...
    'Thank you for your interest in AEO-Light.', ...
    '', ...
    'It appears that you are running a copy that is not', ...
    'intended for the computer on which you are using it.', ...
    '', ...
    'To obtain a suitable copy, please, register your' ...
    'computer or contact Borislav Karaivanov at ', ...
    '', ...
    '                    karaivan@math.sc.edu', ...
    '', ...
    };

if (ispc == true)
    
    %======================================================================
    % STOP. DO NOT EDIT THIS FILE BEFORE READING THE REMARK
    %======================================================================
    
    % Get the computer and user name currently in use.
    [status, userName] = system('whoami');
    % If the attempt failed, then arrange for the application to terminate.
    if (status ~= 0) 
        userName = ['Not' registeredCellArr{1, 1}]; 
    end 
    % Get rid of any characters not on the keyboard. 
    userName = userName(1:find( (userName >= '!')&(userName <= '~') , 1, 'last'));
     
    % If the current user name does not match any of the authorized user
    % names, then close the application. 
    if (nnz(cellfun(@(x) strcmpi(x, userName), registeredCellArr(:, 1))) == 0)
        msgbox(unauthorizedUserMessageCellArr, 'Unauthorized user', 'error');
        isRegisteredUser = false;
    else
        isRegisteredUser = true;
    end 
elseif (ismac == true)
    
    %======================================================================
    % STOP. DO NOT EDIT THIS FILE BEFORE READING THE REMARK
    %======================================================================

    % Get the computer and user name currently in use.
    [status, userName] = system('whoami');
    % If the attempt failed, then arrange for the application to terminate.
    if (status ~= 0) 
        userName = ['Not' registeredCellArr{1, 1}]; 
    end 
    % Get rid of any characters not on the keyboard.
    userName = userName(1:find( (userName >= '!')&(userName <= '~') , 1, 'last'));
     
    % If the current user name does not match any of the authorized user
    % names, then close the application.
    if (nnz(cellfun(@(x) strcmpi(x, userName), registeredCellArr(:, 1))) == 0)
        msgbox(unauthorizedUserMessageCellArr, 'Unauthorized user', 'error');
        isRegisteredUser = false;
    else
        isRegisteredUser = true;
    end 
else
    
    %======================================================================
    % STOP. DO NOT EDIT THIS FILE BEFORE READING THE REMARK
    %======================================================================

    % Get the computer and user name currently in use.
    [status, userName] = system('hostname;whoami');
    % If the attempt failed, then arrange for the application to terminate.
    if (status ~= 0) 
        userName = {['Not' registeredCellArr{1, 1}], ['Not' registeredCellArr{1, 2}]}; 
    end 
    % Get rid of any characters not on the keyboard.
    userName = userName(1:find( (userName >= '!')&(userName <= '~') , 1, 'last'));
    
    % Split the string holding the computer and user names into lines.
    userNameCellArr = regexp(userName, '\n', 'split');
     
    % If the current user name does not match any of the authorized user
    % names, then close the application.
    if (nnz( (cellfun(@(x) strcmpi(x, userNameCellArr{1}), registeredCellArr(:, 1)))&...
            (cellfun(@(x) strcmpi(x, userNameCellArr{2}), registeredCellArr(:, 2))) ) == 0)
        msgbox(unauthorizedUserMessageCellArr, 'Unauthorized user', 'error');
        isRegisteredUser = false;
    else
        isRegisteredUser = true;
    end
end 
 
return; 
% end of the function "isRegistered" 
